package com.zsh.wanandroid.ui.activity;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.PermissionUtils;
import com.dxq.basic_ui_library.TabView;
import com.zsh.wanandroid.R;
import com.zsh.wanandroid.base.BaseActivity;
import com.zsh.wanandroid.base.BaseFragment;
import com.zsh.wanandroid.common.AppManager;
import com.zsh.wanandroid.ui.fragment.HomeFragment;
import com.zsh.wanandroid.ui.fragment.ProjectFragment;
import com.zsh.wanandroid.ui.fragment.SquareFragment;
import com.zsh.wanandroid.ui.fragment.SystemFragment;
import com.zsh.wanandroid.ui.fragment.WeChatFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zsh
 * @date 2020/3/12.
 * 功能：Activity主页面
 */
public class MainActivity extends BaseActivity {

    private ImageView ivMenu;
    private TextView tvTitle;
    private ImageView ivMore;
    private FrameLayout mainFrameLayout;
    private TabView tabHome, tabSquare, tabWeChat, tabSystem, tabProject;
    private List<TabView> tabViews = new ArrayList<>();

    private FragmentManager fragmentManager;
    public BaseFragment currentFragment, homeFragment, squareFragment, weChatFragment, systemFragment, projectFragment;

    private int mCurrentTab;

    private static final String BUNDLE_KEY_POS = "bundle_key_pos";
    private static final String HOME = "home";
    private static final String SQUARE = "square";
    private static final String WECHAT = "wechat";
    private static final String SYSTEM = "system";
    private static final String PROJECT = "project";

    private String[] tabNames = {"首页", "广场", "公众号", "体系", "项目"};

    @Override
    public void initPrams(Bundle prams) {

    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_main;
    }

    @Override
    public void initView(View view) {
        ivMenu = $(R.id.ivMenu);
        tvTitle = $(R.id.tvTitle);
        ivMore = $(R.id.ivMore);
        mainFrameLayout = $(R.id.mainFrameLayout);
        tabHome = $(R.id.tabHome);
        tabSquare = $(R.id.tabSquare);
        tabWeChat = $(R.id.tabWeChat);
        tabSystem = $(R.id.tabSystem);
        tabProject = $(R.id.tabProject);
    }

    @Override
    public void setListener() {
        ivMenu.setOnClickListener(this);
        ivMore.setOnClickListener(this);
        tabHome.setOnClickListener(this);
        tabSquare.setOnClickListener(this);
        tabWeChat.setOnClickListener(this);
        tabSystem.setOnClickListener(this);
        tabProject.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.ivMenu://菜单
                startActivity(MineActivity.class);
                break;

            case R.id.ivMore://更多
                break;

            case R.id.tabHome://首页
                if (null == homeFragment) {
                    homeFragment = new HomeFragment().newInstance(null);
                }
                showFragment(homeFragment, HOME);
                setCurrentTab(0);
                break;

            case R.id.tabSquare://广场
                if (null == squareFragment) {
                    squareFragment = new SquareFragment().newInstance(null);
                }
                showFragment(squareFragment, SQUARE);
                setCurrentTab(1);
                break;

            case R.id.tabWeChat://公众号
                if (null == weChatFragment) {
                    weChatFragment = new WeChatFragment().newInstance(null);
                }
                showFragment(weChatFragment, WECHAT);
                setCurrentTab(2);
                break;

            case R.id.tabSystem://体系
                if (null == systemFragment) {
                    systemFragment = new SystemFragment().newInstance(null);
                }
                showFragment(systemFragment, SYSTEM);
                setCurrentTab(3);
                break;

            case R.id.tabProject://项目
                if (null == projectFragment) {
                    projectFragment = new ProjectFragment().newInstance(null);
                }
                showFragment(projectFragment, PROJECT);
                setCurrentTab(4);
                break;
        }
    }

    @Override
    public void doBusiness(Bundle savedInstanceState) {
        if (savedInstanceState != null)
            mCurrentTab = savedInstanceState.getInt(BUNDLE_KEY_POS, 0);
        initFragment();
        //初始化底部tabView
        initTabViews();
        //默认显示tab
        setCurrentTab(mCurrentTab);
    }

    private void initFragment() {
        //得到FragmentManger
        fragmentManager = this.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        //默认加载第一个fragment
        homeFragment = HomeFragment.newInstance(null);
        currentFragment = homeFragment;
        fragmentTransaction.replace(R.id.mainFrameLayout, currentFragment, HOME);
        fragmentTransaction.commit();
    }

    private void initTabViews() {
        tabHome.setIconAndText(R.drawable.ic_home_grey_24dp, R.drawable.ic_home_black_24dp, tabNames[0]);
        tabSquare.setIconAndText(R.drawable.ic_square_grey_24dp, R.drawable.ic_square_black_24dp, tabNames[1]);
        tabWeChat.setIconAndText(R.drawable.ic_wechat_grey_24dp, R.drawable.ic_wechat_black_24dp, tabNames[2]);
        tabSystem.setIconAndText(R.drawable.ic_sys_grey_24dp, R.drawable.ic_sys_black_24dp, tabNames[3]);
        tabProject.setIconAndText(R.drawable.ic_project_grey_24dp, R.drawable.ic_project_black_24dp, tabNames[4]);
        tabViews.add(tabHome);
        tabViews.add(tabSquare);
        tabViews.add(tabWeChat);
        tabViews.add(tabSystem);
        tabViews.add(tabProject);
    }

    private void showFragment(BaseFragment fg, String tag) {
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        if (!fg.isAdded()) {
            fragmentTransaction.hide(currentFragment).add(R.id.mainFrameLayout, fg, tag);
        } else {
            fragmentTransaction.hide(currentFragment).show(fg);
        }
        //全局变量，记录当前显示的fragment
        currentFragment = fg;
        fragmentTransaction.commit();
    }

    private void setCurrentTab(int pos) {
        for (int i = 0; i < tabViews.size(); i++) {
            TabView tabView = tabViews.get(i);
            if (i == pos) {
                tabView.setProgress(1);
            } else {
                tabView.setProgress(0);
            }
        }
        tvTitle.setText(tabNames[pos]);
        if (pos == 1) {
            ivMore.setImageResource(R.drawable.ic_add_white_24dp);
        } else {
            ivMore.setImageResource(R.drawable.ic_search_white_24dp);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onBackPressed() {
        if (exitApp()) {
            AppManager.getAppManager().finishAllService();
            AppManager.getAppManager().finishAllActivity();
        }
    }
}
